package com.python.project2.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 处理跨域问题的filter
 * @Date 2023/3/31
 * @Author lixian
 **/
//@Component
public class CORSFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain filterChain) throws ServletException, IOException {
		System.out.println(req.getRemotePort());
		System.out.println(req.getRemoteHost());
		//允许来自于哪个域的请求发往后端，写前端的主机、端口号即可
		resp.setHeader("Access-Control-Allow-Origin", "*");
		//发往后端的请求允许何种请求方法
		resp.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE");
		//发送请求时允许携带的请求头
		resp.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type,X-CskaoyanMarket-Admin-Token,X-CskaoyanMarket-Token");
		//发送请求时是否允许携带cookie凭证
		resp.setHeader("Access-Control-Allow-Credentials", "true");
		filterChain.doFilter(req, resp);
	}
}
